﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MultWebApi.Models
{
    public class ConvertHelper
    {
        /// <summary>
        /// 日期字符串转DateTime日期（NULL,空值返回"1900-1-1"）
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(object dt)
        {
            DateTime out_data = new DateTime(2000,1,1);
            if (dt != null)
            {
                if (!DateTime.TryParse(dt.ToString()!.Trim(), out out_data))
                {
                    out_data = new DateTime(2000, 1, 1);
                }
            }
            return out_data.Date;
        }

        /// <summary>
        /// 日期字符串转DateTime日期（NULL,空值返回"1900-1-1"）
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime ToLongDateTime(object dt)
        {
            DateTime out_data = new DateTime(2000, 1, 1);
            if (dt != null)
            {
                if (!DateTime.TryParse(dt.ToString()!.Trim(), out out_data))
                {
                    out_data = new DateTime(2000, 1, 1);
                }
            }
            return out_data;
        }


        /// <summary>
        /// Int32数字字符串转数字（NULL,空值返回0）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Int32 ToInt32(object str, int defValue = 0)
        {
            Int32 out_data = defValue;
            if (str != null)
            {
                if (!Int32.TryParse(str.ToString().Trim(), out out_data))
                {
                    out_data = defValue;
                }
            }
            return out_data;
        }

    }
}
